<script setup lang="ts">
import { useAsyncState } from '@vueuse/core'
import { useRouter } from 'vue-router'
import LoadingContainer from '@/components/common/LoadingContainer.vue'
import ActionCard from '@/components/homepage/ActionCard.vue'
import LatestHomework from '@/components/homepage/LatestHomework.vue'
import { getHomeworkList } from '@/request/teacher/homework'
import { useTeacherStore } from '@/store/teacher'

const router = useRouter()
const teacherStore = useTeacherStore()

// 卡片静态数据
const cardData = [
  {
    imageSrc: '/homework.png',
    text: '布置作业',
    url: '/homework',
  },
  {
    imageSrc: '/manage-class.png',
    text: '新建题组',
    url: '/bank/create',
  },
  {
    imageSrc: '/create-class.png',
    text: '创建班级',
    url: '/class/create-class',
  },
]
// 获取最新作业列表
const { state: latestHomeworkList, isLoading } = useAsyncState(getHomeworkList({
  pageNum: 1,
  pageSize: 3,
}).then(res => res.entry.homeworks.map(item => ({
  id: item.id,
  className: item.clazz,
  title: item.title,
  publishTime: item.publishTime,
  ddl: item.ddl,
  status: item.status === 0 ? '进行中' : '已截止',
  totalStudents: Number(item.allCount),
  completedStudents: Number(item.completionCount),
  uncompletedStudents: Number(item.allCount) - Number(item.completionCount),
  classId: item.classId,
}))), [])

function handleViewHomeworkDetails({ id, classId }: { id: string, classId: string }) {
  // 处理查看作业详情
  router.push(`/homework/detail/${classId}/${id}`)
}
</script>

<template>
  <div class="home-content">
    <div class="content-header">
      {{ teacherStore.teacherName }}老师，欢迎使用小花狮阅读教学系统
    </div>
    <div class="card-list">
      <ActionCard
        v-for="(card, index) in cardData" :key="index" :image-src="card.imageSrc" :text="card.text"
        :url="card.url"
      />
    </div>
    <div class="latest-homework-list-container">
      <div class="latest-homework-list-header">
        <span class="latest-homework-list-title">最新作业列表</span>
        <router-link class="latest-homework-list-more" to="/homework">
          查看更多 &gt;
        </router-link>
      </div>
      <div class="latest-homework-list-content">
        <LoadingContainer :loading="isLoading">
          <div v-if="latestHomeworkList.length === 0" class="no-homework">
            <p v-if="!isLoading">
              暂无作业
            </p>
          </div>
          <div v-else>
            <LatestHomework
              v-for="homework in latestHomeworkList" :key="homework.id" :homework="homework"
              @view-details="handleViewHomeworkDetails"
            />
          </div>
        </LoadingContainer>
      </div>
    </div>
    <img id="background" src="/background.png">
  </div>
</template>

<style scoped>
.no-homework {
  text-align: center;
  padding: 40px 0;
  color: #888888;
  font-size: 14px;
}

.home-content {
  width: 100%;
  position: relative;
  z-index: 1;

  .card-list {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: 32px;
    margin: 2rem 0px;

    &:deep(.action-card) {
      flex: 1;
      min-width: 200px;
    }
  }

  .latest-homework-list-container {
    width: 100%;
    height: fit-content;

    .latest-homework-list-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin: 2rem 0px;

      .latest-homework-list-title {
        font-weight: 500;
        font-size: 18px;
        line-height: 26px;
        letter-spacing: -0.06em;
      }

      .latest-homework-list-more {
        text-decoration: none;
        text-align: right;
        letter-spacing: 0.01em;
        color: var(--primary-color);
      }
    }

    .latest-homework-list-content {
      width: 100%;
      height: fit-content;
    }
  }
}

#background {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 55%;
  height: auto;
  z-index: 0;
  pointer-events: none;
}
</style>
